package com.zhilianbao.leyaogo.ui.adapter.me;

import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import android.widget.TextView;

import com.zhilianbao.leyaogo.R;
import com.zhilianbao.leyaogo.model.response.me.HelpResponse;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by liu on 2016/10/27.
 * 客服与帮助的adapter
 */
public class ServiceHelpAdapter extends BaseExpandableListAdapter {
    Context mContext;
    List<HelpResponse> list;
    private  List<HelpResponse.ChildrenClassifyListBean> mChildrenClassifyList = new ArrayList<>();
    private List<HelpResponse.ChildrenClassifyListBean.SupplierHelpContentListBean> mSupplierHelpContentList = new ArrayList<>();

    public ServiceHelpAdapter(Context context, List<HelpResponse> list) {
        this.mContext = context;
        this.list = list;
        getContentList(list);
//        mChildrenClassifyList = list.get(0).getChildrenClassifyList();
    }

    private void getContentList(List<HelpResponse> list) {
        if (list == null || list.size() == 0) {
            return;
        }
        for (int i = 0; i < list.size(); i++) {
            HelpResponse helpResponse = list.get(i);
            mChildrenClassifyList.addAll(helpResponse.getChildrenClassifyList());
        }
        if (mChildrenClassifyList != null && mChildrenClassifyList.size() != 0) {
            for (int i = 0; i < mChildrenClassifyList.size(); i++) {
                HelpResponse.ChildrenClassifyListBean childrenClassifyListBean = mChildrenClassifyList.get(i);
                mSupplierHelpContentList.addAll(childrenClassifyListBean.getSupplierHelpContentList()) ;
            }
        }
    }

  /*  //获取一级菜单条目的总数
    @Override
    public int getGroupCount() {
        if (list!=null && mChildrenClassifyList != null) {
            return mChildrenClassifyList.size();
        }
        return 0 ;
    }

    //获取二级菜单条目的总数
    @Override
    public int getChildrenCount(int groupPosition) {
        if (list != null && mChildrenClassifyList.get(groupPosition).getSupplierHelpContentList() != null){
           return list.get(0).getChildrenClassifyList().get(groupPosition).getSupplierHelpContentList().size();
        }
        return 0;
    }

    //获取一级菜单的一个条目
    @Override
    public Object getGroup(int groupPosition) {
        if (list!=null && mChildrenClassifyList != null) {
            return mChildrenClassifyList.get(groupPosition);
        }
        return null ;
    }

    //获取二级菜单的一个条目
    @Override
    public Object getChild(int groupPosition, int childPosition) {
        if (list != null && mChildrenClassifyList.get(groupPosition).getSupplierHelpContentList() != null){
            return mChildrenClassifyList.get(groupPosition).getSupplierHelpContentList().get(childPosition);
        }
        return null;
    }

    //获取一级菜单的id
    @Override
    public long getGroupId(int groupPosition) {
        if (list!=null && mChildrenClassifyList != null) {
            return groupPosition;
        }
        return 0 ;
    }

    //获取二级菜单的id
    @Override
    public long getChildId(int groupPosition, int childPosition) {
        if (list != null && mChildrenClassifyList.get(groupPosition).getSupplierHelpContentList() != null){
            return childPosition;
        }
        return 0;
    }
*/

    //获取一级菜单条目的总数
    @Override
    public int getGroupCount() {
        if (mSupplierHelpContentList!=null) {
            return mSupplierHelpContentList.size();
        }
        return 0 ;
    }

    //获取二级菜单条目的总数
    @Override
    public int getChildrenCount(int groupPosition) {
        if (mSupplierHelpContentList!=null) {
            return 1;
        }
        return 0 ;
    }

    //获取一级菜单的一个条目
    @Override
    public Object getGroup(int groupPosition) {
        if (mSupplierHelpContentList!=null) {
            return mSupplierHelpContentList.get(groupPosition);
        }
        return null ;
    }

    //获取二级菜单的一个条目
    @Override
    public Object getChild(int groupPosition, int childPosition) {
        if (mSupplierHelpContentList!=null) {
            return mSupplierHelpContentList.get(childPosition);
        }
        return null ;
    }

    //获取一级菜单的id
    @Override
    public long getGroupId(int groupPosition) {
        if (mSupplierHelpContentList!=null ) {
            return groupPosition;
        }
        return 0 ;
    }

    //获取二级菜单的id
    @Override
    public long getChildId(int groupPosition, int childPosition) {
        if (mSupplierHelpContentList != null){
            return childPosition;
        }
        return 0;
    }


    @Override
    public boolean hasStableIds() {
        return false;
    }

    //获取一级菜单的视图
    @Override
    public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {
        ViewHolderGroup group;
        if (convertView == null) {
            group = new ViewHolderGroup();
            convertView = View.inflate(mContext, R.layout.item_service_help, null);
            group.title = (TextView) convertView.findViewById(R.id.help_title);
            convertView.setTag(group);
        } else {
            group = (ViewHolderGroup) convertView.getTag();
        }
        group.title.setText(mSupplierHelpContentList.get(groupPosition).getHelpTitle());
        return convertView;
    }

    //获取二级菜单的视图
    @Override
    public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup parent) {
        ViewHolderChild child;
        if (convertView == null) {
            child = new ViewHolderChild();
            convertView = View.inflate(mContext, R.layout.item_ser_help_content, null);
            child.content = (TextView) convertView.findViewById(R.id.help_content);
            convertView.setTag(child);
        } else {
            child = (ViewHolderChild) convertView.getTag();
        }
        child.content.setText(mSupplierHelpContentList.get(groupPosition).getHelpContent());

        return convertView;
    }

    //决定子视图是否可以被点击
    @Override
    public boolean isChildSelectable(int groupPosition, int childPosition) {
        return false;
    }

    class ViewHolderGroup {
        TextView title;
    }

    class ViewHolderChild {
        TextView content;
    }
}